//
//  HightLightsViewController.m
//  MaskMachine
//
//  Created by yuanyuan on 15-3-16.
//  Copyright (c) 2015年 hanjiaxue. All rights reserved.
//

#import "HightLightsViewController.h"
#import "WaterFallScrollView.h"
#import "WaterShop.h"
#import "WaterFallViewCell.h"
#import "WaterShopCell.h"
#import "AFHTTPRequestOperationManager.h"
#import "MJRefresh.h"
#import "InterFace.h"
#import "UIViewAdditions.h"
#import "AllPicViewController.h"
#import "YoungAllData.h"
#import "GMDCircleLoader.h"
#define LoadMessagesCount 15

@interface HightLightsViewController ()<YoungDelegate>{
    int a;
}
@property(nonatomic,retain)NSMutableArray *zArrayImage;
@end

@implementation HightLightsViewController
@synthesize productStr,productName,zArrayImage;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(NSMutableArray *)shops
{
    if (_shops==nil) {
        _shops=[NSMutableArray array];
    }
    return _shops;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    a = 0;
    
    self.navigationController.navigationBarHidden=YES;
    self.view.backgroundColor = [self colorWithHexString:@"#F5F5F5"];
    [self initNav];
    navTitle.text = productName;
    
    
    UIImageView *imagevie = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 42)];
    imagevie.image = [UIImage imageNamed:@"imgDataDetil"];
    [self.view addSubview:imagevie];
    
    YoungAllData *data = [[YoungAllData alloc]init];
    data.delegate = self;
    [data gainImage:productStr andPageID:@"1" andPerPage:@"15"];
    [GMDCircleLoader setOnView:self.view withTitle:@"加载中···" animated:YES];
}
#pragma mark - 获取图片素材回调
-(void)images:(NSDictionary *)sign{
    [GMDCircleLoader hideFromView:self.view animated:YES];
    if ([[sign objectForKey:@"status"] isEqualToString:@"success"]) {
    zArrayImage = [[NSMutableArray alloc]initWithArray:[sign objectForKey:@"data"]];
    //2.创建一个瀑布流
    _waterflow=[[WaterFallScrollView alloc]init];
    _waterflow.frame=CGRectMake(0,navView.bottom, SCREEN_WIDTH,SCREEN_HEIGHT-64-44);
    _waterflow.delegate=self;
    _waterflow.userInteractionEnabled = YES;
    _waterflow.dadaSource=self;
    [self.view addSubview:_waterflow];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:[sign objectForKey:@"reason"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];

    }
}
#pragma mark - 获取图片素材回调失败
-(void)requistFail{
    [GMDCircleLoader hideFromView:self.view animated:YES];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"网络异常,无法加载数据" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];

}

#pragma mark - 返回
-(void)clickBackBtn{
    [self.navigationController popViewControllerAnimated:NO];
}


-(void)downloadDatadetailImgsHightLights:(NSArray *)array{
    NSLog(@" %@",array);
    //    [self.shops removeAllObjects];‘
    self.allArray = [NSArray arrayWithArray:array];
    self.shops = [NSMutableArray array];
    if (array.count>LoadMessagesCount) {
        for (int i = 0; i<LoadMessagesCount; i++) {
            [self.shops addObject:[array objectAtIndex:i]];
        }
    }else{
        self.shops = [NSMutableArray arrayWithArray:array];
    }
    
    
    
    //[self.waterflow addFooterWithTarget:self action:@selector(loadMoreShops)];
    
    //
    //    UIImageView *ab = (UIImageView *)[self.view viewWithTag:90];
    //    ab.hidden = YES;
    
}

//加载
-(void)loadMoreShops{
    
    //    [self.shops removeAllObjects];
    a++;
    
    
    if (self.allArray.count>LoadMessagesCount*a) {
        
        if (self.allArray.count<LoadMessagesCount*(a+1)) {
            for (int i = LoadMessagesCount*a; i<self.allArray.count; i++) {
                [self.shops addObject:[self.allArray objectAtIndex:i]];
            }
        }else{
            for (int i = LoadMessagesCount*a; i<LoadMessagesCount*(a+1); i++) {
                [self.shops addObject:[self.allArray objectAtIndex:i]];
            }
        }
        
        
        
    }
    
    
    
    
    
    //    [self.shops addObjectsFromArray:arrays];
    
    NSLog(@"瑟利夫 --- %@",self.shops);
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.waterflow reloadData];
        
        //[self.waterflow footerEndRefreshing];
    });
    
    
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark-数据源方法
-(NSUInteger)numberOfCellsInWaterflowView:(WaterFallScrollView *)waterflowView
{
    
    NSLog(@"%lu =-=-=  seldjfla",(unsigned long)zArrayImage.count);
    
    return zArrayImage.count;
}
-(NSUInteger)numberOfColumnsInWaterflowView:(WaterFallScrollView *)waterflowView
{
    return 2;
}
-(WaterFallViewCell *)waterflowView:(WaterFallScrollView *)waterflowView cellAtIndex:(NSUInteger)index
{
    
    WaterShopCell *cell=[WaterShopCell cellWithwaterflowView:waterflowView];
    cell.shop=[[WaterShop alloc] initWithDictionary:zArrayImage[index]];
    
    return cell;
}


#pragma mark-代理方法
-(CGFloat)waterflowView:(WaterFallScrollView *)waterflowView heightAtIndex:(NSUInteger)index
{
    WaterShop *shop=[[WaterShop alloc] initWithDictionary:zArrayImage[index]];
    
    NSLog(@"%f -- gao  %f -- kuan",shop.h,shop.w);
    
    
    //根据Cell的宽度和图片的宽高比 算出cell的高度
    return waterflowView.cellWidth*shop.h/shop.w;
}

-(void)waterflowView:(WaterFallScrollView *)waterflowView didSelectAtIndex:(NSUInteger)index
{
    //    NSLog(@"点击了第%d个cell",index);
    //    NSLog(@"%@ deeiiiiekke",self.shops[index]);
    //
    //    float h = [[self.shops[index] objectForKey:@"height"] floatValue];
    //    float w = [[self.shops[index] objectForKey:@"width"] floatValue];
    
    AllPicViewController *ampl = [[AllPicViewController alloc] init];
    ampl.imgName  = [zArrayImage[index] objectForKey:@"ImagePath"];
    ampl.zWidth = [[zArrayImage[index] objectForKey:@"Width"] floatValue];
    ampl.zHeight = [[zArrayImage[index] objectForKey:@"Height"] floatValue];
    [self.navigationController pushViewController:ampl animated:YES];
    
    
}
-(void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = NO;
}
-(void)dealloc{
    self.imgURL = nil;
    self.shops = nil;
    self.allArray = nil;
    self.waterflow = nil;
    
}

@end
